﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet.InPacket;
using Hi_Baidu.Packet.InPacket.UpdateUserInfo;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Packet.InPacket.TalkInPacket;
using Hi_Baidu.Packet.InPacket.TalkInPacket.Hole;
using Hi_Baidu.Enum.User;

namespace Hi_Baidu.Packet
{
    class TalkMessagePacketListener : IPacketListener
    {
        private delegate void TOTAL_MESSAGE(Total_TalkMessagePacket Args);
        private delegate void DETACH_MESSAGE(Detach_TalkMessagePacket Args);
        private delegate void DETACH_IMAGE(Detach_TalkImagePacket Args);
        private delegate void WINDOW_SHADOW(WindowShadowPacket Args);
        private delegate void ACK_WINDOW_SHADOW(AckSucessfulWindowShadowPacket Args);

        #region IPacketListener 成员

        public bool Accpet(InPackage pck)
        {
            return pck.GetProtocol() == Protocol.NetworkProtocol.TALK_MESSAGE;
        }

        public void PackageArrived(InPackage pck)
        {
            if (Globle.user.USER_ONLINE_STATE != ONLIN_STATE.OFFLINE)
          {
              switch ((pck as TalkMessageInPackage).Command)
              {
                  //完整聊天信息
                  case Protocol.TALK_IN_NetworkCommand.TOTAL_TALK_MESSAGE:
                      System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new TOTAL_MESSAGE(ChatViewModel.ReadTalkMessage), new Hi_Baidu.Packet.InPacket.TalkInPacket.Total_TalkMessagePacket(pck as TalkMessageInPackage));
                      break;
                  //分片聊天信息
                  case Protocol.TALK_IN_NetworkCommand.DETACH_TALK_MESSAGE:
                      System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new DETACH_MESSAGE(ChatProcessModel.ProcessDetachMessage), new Detach_TalkMessagePacket(pck as TalkMessageInPackage));
                      break;
                  //卸载成功的通话超时器
                  case Protocol.TALK_IN_NetworkCommand.REPLY_SUCESSFUL_TALK_MESSAGE:
                      ChatProcessModel.UnInstallTimer_SucessfulTalkMessage(new AckSucessfulTalkMessagePacket(pck as TalkMessageInPackage));
                      break;
                  //图片提取请求包
                  case Protocol.TALK_IN_NetworkCommand.REQUEST_PICTURE:
                      ChatProcessModel.ReSendRequestImage(new RequestPicturePacket(pck as TalkMessageInPackage));
                      break;
                  //分片图片信息
                  case Protocol.TALK_IN_NetworkCommand.DETACH_IMAGE:
                      System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new DETACH_IMAGE(ChatProcessModel.ProcessDetachImage), new Detach_TalkImagePacket(pck as TalkMessageInPackage));
                      break;
                  //窗口抖动请求
                  case Protocol.TALK_IN_NetworkCommand.WINDOW_SHADOW:
                      System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new WINDOW_SHADOW(ChatProcessModel.ProcessWindowShadow), new WindowShadowPacket(pck as TalkMessageInPackage));
                      break;
                  //回馈窗口抖动
                  case Protocol.TALK_IN_NetworkCommand.ACK_WAINDOW_SHADOW:
                      System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ACK_WINDOW_SHADOW(ChatProcessModel.ACK_WindowShadow), new AckSucessfulWindowShadowPacket(pck as TalkMessageInPackage));
                      break;
                  //由服务器转发的打洞请求
                  case Protocol.TALK_IN_NetworkCommand.REDIRECT_HOLE:
                      ClientProcessModel.Process_RedirectHole(new RedirectHolePacket(pck as TalkMessageInPackage));
                      break;
                  //好友的打洞测试
                  case Protocol.TALK_IN_NetworkCommand.TEST_HOLE:
                      ClientProcessModel.Process_TestHole(new TestHolePacket(pck as TalkMessageInPackage));
                      break;
                  //服务器反馈的打洞结果
                  case Protocol.TALK_IN_NetworkCommand.SERVER_ACK_HOLE:
                      ClientProcessModel.ProcessAckServerHole(new AckServerHolePacket(pck as TalkMessageInPackage));
                      break;
              }
          }
        }
        #endregion
    }
}
